package com.wyl.main.alarm

import androidx.databinding.ObservableArrayList
import com.wyl.base.ACacheHelper
import com.wyl.base.ALARM_LIST
import com.wyl.libbase.base.PageViewModel
import com.wyl.main.bean.AlarmData
import java.util.*

class AlarmViewModel : PageViewModel() {
    val dataSource = ObservableArrayList<AlarmData>()

    init {
        enableRefresh.set(false)
        enableLoadMore.set(false)
        readAlarmList()
    }


    override fun refresh() {

    }

    override fun loadMore() {

    }

    fun addAlarmData(hour: Int, minute: Int) {
        val calendar = Calendar.getInstance().apply {
            set(Calendar.HOUR_OF_DAY, hour)
            set(Calendar.MINUTE, minute)
        }
        dataSource.add(AlarmData(calendar = calendar))

        // 持久化存储
        saveAlarmList()
    }

    private fun readAlarmList() {
//        val list = ACacheHelper.cache.getAsObject(ALARM_LIST) as ArrayList<AlarmData>

        ACacheHelper.cache.getAsObject(ALARM_LIST)?.let {
            dataSource.addAll(it as ArrayList<AlarmData>)
        }
    }

    private fun saveAlarmList() {
        ACacheHelper.cache.put(ALARM_LIST, dataSource)
    }

}
